﻿
CREATE FUNCTION dbo.Score_TriMerge
	(
	@Score1 real,
	@Score2 real,
	@Score3 real
	)
RETURNS smallint
AS
BEGIN

DECLARE @Score int

SET @Score = dbo.Score_Count(@Score1,@Score2,@Score3)

IF @Score = 3 
	SELECT @Score1 = ROUND(AVG(Score),0) FROM(SELECT @Score1 Score UNION SELECT @Score2 UNION SELECT @Score3)T

IF @Score = 2
	SELECT @Score1 = MIN(Score) FROM(SELECT @Score1 Score UNION SELECT @Score2 UNION SELECT @Score3)T

RETURN COALESCE(@Score1,@Score2,@Score3)
END

